<template>
  <gc-dialog
    title="文章审核"
    :dialog-visible="true"
    @cancel="handleCancle"
    @ok="handleConfirm"
  >
    <el-form
      ref="formRef"
      label-position="top"
      :model="form"
      :rules="formRules"
    >
      <el-form-item label="审核结果" prop="status">
        <el-radio-group v-model="form.status" class="ml-4">
          <el-radio label="2" size="large">通过</el-radio>
          <el-radio label="3" size="large">不通过</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="不通过原因" prop="reason" v-if="form.status === '3'">
        <el-input
          v-model="form.reason"
          placeholder="请输入不通过原因"
          type="textarea"
          resize="none"
          :rows="4"></el-input>
      </el-form-item>
    </el-form>
  </gc-dialog>
</template>

<script setup lang="ts" name="ApproveArticle">
import { ref, reactive } from 'vue'
import { ruleRequired, ruleMaxLength } from '@/utils/rules'
import type { FormInstance, FormRules } from 'element-plus'
import { apiApproveArticle, apiReapproveArticle } from '@/http/service'
import { ElMessage } from 'element-plus'

const props = defineProps({
  id: Number,
  isAgain: Boolean
})
const emit = defineEmits(['close-modal'])
const formRef = ref<null | FormInstance>(null)
const form = reactive({
  status: '2',
  reason: ''
})
const formRules = reactive<FormRules>({
  status: [ruleRequired('请选择审核结果')],
  reason: [ruleRequired('请输入不通过原因'), ruleMaxLength(255)]
})

async function handleConfirm () {
  const valid = await formRef.value?.validate()
  if (valid) {
    const searchParams = {
      articleId: props.id,
      ...form
    }
    const API = props.isAgain ? apiReapproveArticle : apiApproveArticle
    await API(searchParams)
    ElMessage.success('审核文章成功')
    emit('close-modal', true)
  }
}

function handleCancle () {
  emit('close-modal')
}
</script>

<style scoped lang="scss"></style>
